import os
from web_test.page.app import App
from utils.data_path_manager import data_path_manager
from web_test.page.base_page import BasePage
from utils.logger import logger


class LoginPage(App):
    page_function_file = os.path.join(data_path_manager.page_data_dir, "login", "login_page_functions.yml")

    def login(self, username, password):
        params = {
            "username": username,
            "password": password
        }
        logger.info(f"Log in Feishu.  Params={params}")
        self.perform_functions(self.page_function_file, "login", params)

    def login_with_cookies(self, fs_cookies=None):
        # self.driver.get("https://test-ccg00sirf18q.feishu.cn/admin/index")
        # self.driver.get("https://wjvzp7x5tz.feishu.cn/admin/index")
        fs_cookies = [{'domain': '.feishu.cn', 'expiry': 1663928814, 'httpOnly': False, 'name': 'swp_csrf_token', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': 'c80558e0-0e9f-466d-91ca-1a885d86d2a3'}, {'domain': '.feishu.cn', 'expiry': 1725704452, 'httpOnly': True, 'name': 'session_list', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': 'XN0YXJ0-697i64ec-4ad5-4e45-aed6-c3728b597dfe-WVuZA_XN0YXJ0-697i64ec-4ad5-4e45-aed6-c3728b597dfe-WVuZA'}, {'domain': '.feishu.cn', 'expiry': 1725704452, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False, 'value': 'GA1.1.1562602552.1662622148'}, {'domain': '.feishu.cn', 'expiry': 1725704814, 'httpOnly': True, 'name': 'passport_web_did', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': '7110516554546266116'}, {'domain': '.feishu.cn', 'expiry': 1725704452, 'httpOnly': True, 'name': 'session', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': 'XN0YXJ0-697i64ec-4ad5-4e45-aed6-c3728b597dfe-WVuZA'}, {'domain': '.feishu.cn', 'expiry': 1662805656, 'httpOnly': False, 'name': 'csrf_token', 'path': '/', 'sameSite': 'Lax', 'secure': True, 'value': '61aa7e2d-14db-4137-a65a-296c163e5b20'}, {'domain': '.feishu.cn', 'expiry': 1662803357, 'httpOnly': False, 'name': 'landing_url', 'path': '/', 'secure': True, 'value': 'https://test-ccg00sirf18q.feishu.cn/accounts/page/login?redirect_uri=https%3A%2F%2Ftest-ccg00sirf18q.feishu.cn%2Fadmin%2Findex&app_id=13'}, {'domain': '.feishu.cn', 'expiry': 1662805656, 'httpOnly': True, 'name': 'm_65f68ea2', 'path': '/', 'sameSite': 'Lax', 'secure': True, 'value': '36316161376532642d313464622d343133372d613635612d32393663313633653562323046111c051538e0cc44e1d1f309c607eaed4c1984060d7ebff8b389a1905e6d7d'}, {'domain': 'wjvzp7x5tz.feishu.cn', 'expiry': 1667816293, 'httpOnly': False, 'name': 's_v_web_id', 'path': '/', 'secure': False, 'value': 'verify_l7swc8gn_iqb5OtP4_aLrn_44Sk_9ecO_v6LSPgDyLDvR'}, {'domain': '.feishu.cn', 'expiry': 1725704452, 'httpOnly': False, 'name': '_ga_VPYRHN104D', 'path': '/', 'secure': False, 'value': 'GS1.1.1662632293.1.1.1662632452.0.0.0'}, {'domain': '.feishu.cn', 'expiry': 1670408294, 'httpOnly': False, 'name': '__tea__ug__uid', 'path': '/', 'secure': False, 'value': '7140951270395545129'}, {'domain': '.feishu.cn', 'expiry': 1662718702, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False, 'value': 'GA1.2.1536582556.1662632293'}, {'domain': '.wjvzp7x5tz.feishu.cn', 'expiry': 1725704294, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False, 'value': 'GA1.3.408598172.1662632293'}, {'domain': '.feishu.cn', 'expiry': 1725704292, 'httpOnly': True, 'name': 'QXV0aHpDb250ZXh0', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': 'a6a33cc8a73044f9b155a6227e7026d1'}, {'domain': '.feishu.cn', 'expiry': 3810115949, 'httpOnly': True, 'name': 'trust_browser_id', 'path': '/', 'secure': False, 'value': '471ce520-d809-4b71-a776-99d19d0fedd8'}, {'domain': '.feishu.cn', 'expiry': 1694168856, 'httpOnly': False, 'name': 'locale', 'path': '/', 'secure': True, 'value': 'zh-CN'}, {'domain': '.feishu.cn', 'expiry': 1663928814, 'httpOnly': True, 'name': 't_beda37', 'path': '/', 'sameSite': 'None', 'secure': True, 'value': 'd88c8987664255988074f23500df0d6bbd29a20bb584b2a472b2680ee90f9602'}, {'domain': '.wjvzp7x5tz.feishu.cn', 'expiry': 1662718694, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False, 'value': 'GA1.3.1536582556.1662632293'}]
        # cookies = self.driver.get_cookies()
        # print(cookies)
        for cookie in fs_cookies:
            self.driver.add_cookie(cookie)
        self.driver.refresh()



if __name__ == '__main__':
    login = LoginPage()
    login.login_with_cookies()
